import { reactive, ref } from 'vue'
import { uploadFileApi,submitCertificationAPI,getCertificationDataAPI,updateCertificationAPI } from '@/api'
import storage  from '@/utils/storage'
import BASE_URL from '@/utils/env'
export function useCertificationData() {
    const certificateData = reactive({
        id: null,
        userName: '',
        userPhoto: '',
        phone: '',
        idNumber: '',
        cardFrontPhoto: '',
        cardBackPhoto: '',
        address: '',
        sex: 0,
        userId: '',
        age: 18,
        state: -2
    })
    const userPhotoFile = reactive({
        name:"",
        extname: "",
        url:"",
    })
    const cardFrontFile = reactive({
        name:"",
        extname: "",
        url:"",
    })
    const cardBackFile = reactive({
        name:"",
        extname: "",
        url:"",
    })
    function handleSelect(typeId,file) {
        const [ uploadFilePath ] = file?.tempFilePaths
        uploadFileApi(uploadFilePath,'certificate-file')
            .then(res => {
                if(typeId === 1) {
                    certificateData.userPhoto = res
                } else if(typeId === 2) {
                    certificateData.cardFrontPhoto = res
                } else{
                    certificateData.cardBackPhoto = res
                }
                uni.showToast({title: '上传成功', icon: 'none'})
            })
    }

    const handleCertificateSubmit = (state = 0, action = '提交') => {
        uni.showModal({
            content: `是否${action}认证信息`,
            success: async (res) => {
                if(res.confirm) {
                    const { id } = storage.get('userInfo')
                    certificateData.userId = id
                    try {
                        uni.showLoading({ title: '提交中...'})
                        if(certificateData.id) {
                            certificateData.state = state
                            await updateCertificationAPI(certificateData)
                        } else {
                            await submitCertificationAPI(certificateData)()
                        }
                        uni.showToast({title: '操作成功',icon: 'none'})
                        getCertificationData()
                        uni.hideLoading()
                    }catch (e) {

                    }
                }
            }
        })
    }

    function getCertificationData() {
       getCertificationDataAPI().then(res => {
            if(res) {
                for(const key in certificateData) {
                        certificateData[key] = res[key]
                }
                if(certificateData.userPhoto) {
                    getCertificationFile(userPhotoFile,certificateData.userPhoto)
                }
                if(certificateData.cardFrontPhoto) {
                    getCertificationFile(cardFrontFile,certificateData.cardFrontPhoto)
                }
                if(certificateData.cardBackPhoto) {
                    getCertificationFile(cardBackFile,certificateData.cardBackPhoto)
                }
            }
        })
    }

    function getCertificationFile(fileData,originData) {
        const fileName = originData.split('/').reverse()[0]
        fileData.name = fileName
        fileData.extname = fileName.split('.').reverse()[0]
        fileData.url = `${BASE_URL}images/${originData}`
    }
    return {
        certificateData,
        handleSelect,
        handleCertificateSubmit,
        getCertificationData,
        userPhotoFile,
        cardFrontFile,
        cardBackFile,
    }
}
